﻿namespace Digitalmes.Application.Sys.Posts.Commands;

/// <summary>
/// 更新职位命令。
/// </summary>
public record UpdatePosCommand(SysPosDto Pos) : IRequest<IResult<bool>>;

internal sealed class UpdatePosCommandHandler(IRepository<SysPos> repo) : IRequestHandler<UpdatePosCommand, IResult<bool>>
{
    public async Task<IResult<bool>> Handle(UpdatePosCommand request, CancellationToken cancellationToken)
    {
        // 检查编号是否已存在。
        if (await repo.IsAnyAsync(m => m.Code == request.Pos.Code && m.Id != request.Pos.Id, cancellationToken))
        {
            return Result.Failure("职位编号已存在");
        }

        var pos = request.Pos.Adapt<SysPos>();
        var ok = await repo.UpdateAsync(pos, m => new
        {
            m.Name,
            m.Order,
            m.Status,
            m.UpdatedBy,
            m.UpdatedTime,
        }, cancellationToken);
        return Result.From(ok);
    }
}
